var domain = require('domain');
var express = require('express');
var app = express();
var dnsServer = require('./dns_server');

var ysServerInfo;

var d = domain.create();
d.on('error', function(er) {
  console.error('Caught error!', er);
});
d.run(function() {
  app.get('/sendinfo', function(req, res){
    ysServerInfo = {
      ip : req.ip,
      date : new Date()
    };
    res.send('success');
  });

  app.get('/serverip', function(req, res){
    if(ysServerInfo){
      res.send('服务器地址：' + ysServerInfo.ip + '   发送时间：' + ysServerInfo.date);
    }else{
      res.send('can not get ip address');
    }
  });

  app.listen(8888);

  dnsServer.setIP('www.baidu.com', '127.0.0.1');
  dnsServer.start({
    port : 53
  });
  // dnsServer.start();

});